package com.sls.game.service.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.DataConversion;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.GoodsInfoManageDAO;
import com.sls.game.dao.GoodsSupplyManageDAO;
import com.sls.game.dao.NutsNetGoodsInfoDAO;
import com.sls.game.dao.OhFeiGoodsInfoDAO;
import com.sls.game.dao.TencentGoodsInfoDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.GoodsManageInfo;
import com.sls.game.pojo.GoodsSupplyManageInfo;
import com.sls.game.pojo.NutsNetGoodsInfo;
import com.sls.game.pojo.OhFeiGoodsInfo;
import com.sls.game.pojo.TencentGoodsInfo;
import com.sls.game.service.GoodsSupplyManageService;

/**
 * 
 * @description 商品供应表操作
 * @author Wangxuegang
 * @date 2012-9-18
 * 
 */
@Service("goodsSupplyManageService")
public class GoodsSupplyManageServiceImpl implements GoodsSupplyManageService {

	private static Logger log = Logger
			.getLogger(OriginalGoodsServiceImpl.class);

	private Gson gson = null;

	public GoodsSupplyManageServiceImpl() {
		gson = CommonUtil.gson();
	}

	private GoodsSupplyManageDAO goodsSupplyManageDAO;

	private OhFeiGoodsInfoDAO ohFeiGoodsInfoDAO;

	private NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO;

	private TencentGoodsInfoDAO tencentGoodsInfoDAO;

	private GoodsInfoManageDAO goodsInfoManageDAO;

	public GoodsInfoManageDAO getGoodsInfoManageDAO() {
		return goodsInfoManageDAO;
	}

	@Resource(name = "goodsInfoManageDAO")
	public void setGoodsInfoManageDAO(GoodsInfoManageDAO goodsInfoManageDAO) {
		this.goodsInfoManageDAO = goodsInfoManageDAO;
	}

	public GoodsSupplyManageDAO getGoodsSupplyManageDAO() {
		return goodsSupplyManageDAO;
	}

	@Resource(name = "goodsSupplyManageDAO")
	public void setGoodsSupplyManageDAO(
			GoodsSupplyManageDAO goodsSupplyManageDAO) {
		this.goodsSupplyManageDAO = goodsSupplyManageDAO;
	}

	public OhFeiGoodsInfoDAO getOhFeiGoodsInfoDAO() {
		return ohFeiGoodsInfoDAO;
	}

	@Resource(name = "ohFeiGoodsInfoDAO")
	public void setOhFeiGoodsInfoDAO(OhFeiGoodsInfoDAO ohFeiGoodsInfoDAO) {
		this.ohFeiGoodsInfoDAO = ohFeiGoodsInfoDAO;
	}

	public NutsNetGoodsInfoDAO getNutsNetGoodsInfoDAO() {
		return nutsNetGoodsInfoDAO;
	}

	@Resource(name = "nutsNetGoodsInfoDAO")
	public void setNutsNetGoodsInfoDAO(NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO) {
		this.nutsNetGoodsInfoDAO = nutsNetGoodsInfoDAO;
	}

	public TencentGoodsInfoDAO getTencentGoodsInfoDAO() {
		return tencentGoodsInfoDAO;
	}

	@Resource(name = "tencentGoodsInfoDAO")
	public void setTencentGoodsInfoDAO(TencentGoodsInfoDAO tencentGoodsInfoDAO) {
		this.tencentGoodsInfoDAO = tencentGoodsInfoDAO;
	}

	/**
	 * 分页查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryPageInquire(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsSupplyManageInfo info = gson.fromJson(body,
				GoodsSupplyManageInfo.class);
		boolean flag = checkDataOfqueryByPage(info, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				String resultBody = null;
				Pagination<GoodsSupplyManageInfo> paginationList = null;
				paginationList = goodsSupplyManageDAO.queryPaginationList(info);

				if (paginationList != null) {
					resultBody = gson.toJson(paginationList);
				}

				log.info("分页查询结果为：  " + resultBody);
				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}
				return resultBody;
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 记录新增
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String insert(String body, Head head) throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsSupplyManageInfo info = gson.fromJson(body,
				GoodsSupplyManageInfo.class);
		boolean flag = checkDataOfInsert(info, head.getRequestId());
		if (flag) { // 校验通过
			// 商品编号
			String goodsId = info.getGoodsId();
			// 供应商编号
			String supplyId = info.getSuppliersId();
			// 原始商品编号
			String originalGoodsId = info.getOriginalGoodsId();

			GoodsSupplyManageInfo info1 = new GoodsSupplyManageInfo();
			info1.setId(UUID.randomUUID().toString());
			info1.setGoodsId(goodsId);
			info1.setOriginalGoodsId(originalGoodsId);
			info1.setOperator(info.getOperator());

			// 根据商品编号，查询商品信息表
			GoodsManageInfo goodsManageInfo = null;
			goodsManageInfo = goodsInfoManageDAO
					.queryGoodsInfoByGoodsId(goodsId);
			if (goodsManageInfo != null) {
				info1.setGoodsCategoryId(goodsManageInfo.getGoodsCategoryId());
				info1.setGoodsName(goodsManageInfo.getGoodsName());
				info1.setGoodsDescription(goodsManageInfo.getGoodsDescription());
				info1.setOfficialUrl(goodsManageInfo.getOfficialUrl());
				info1.setRechargeMode(goodsManageInfo.getRechargeMode());
				info1.setEnable(goodsManageInfo.getEnable());
				info1.setRechargeArea(goodsManageInfo.getRechargeArea());
				info1.setBrandName(goodsManageInfo.getBrandName());

			} else {
				log.error("Service处理[" + head.getRequestId()
						+ "]的请求出现异常，商品信息表中该商品不存在！");
				throw new ServerDisponseException(
						ExceptionCodeDes.DATABASE_RECORDS_NOT_EXITS.getCode(),
						ExceptionCodeDes.DATABASE_RECORDS_NOT_EXITS
								.getDescription());
			}

			// 插入时间
			String now = ServerDateAndTime.getNowDateTimeOfFormat();
			info1.setInsertTime(now);
			GoodsSupplyManageInfo goodsSupplyManageInfo = null;
			GoodsSupplyManageInfo goodsSupplyManageInfo1 = new GoodsSupplyManageInfo();
			goodsSupplyManageInfo1.setSuppliersId(supplyId);
			goodsSupplyManageInfo1.setOriginalGoodsId(originalGoodsId);

			/**
			 * 插入之前，先判断即将插入的商品记录是否存在
			 */
			goodsSupplyManageInfo = goodsSupplyManageDAO
					.queryByGoodsIdAndSupplyId(goodsSupplyManageInfo1);
			if (goodsSupplyManageInfo == null) { // 记录 不存在
				if ("20121000".equals(supplyId)) { // 殴飞
					addOhFeiGoods(info, info1, head);

				} else if ("20121001".equals(supplyId)) { // 坚果网络
					addNutsNetGoods(info, info1, head);

				} else if ("20121002".equals(supplyId)) { // 腾讯
					addTencentGoods(info, info1, head);
				}

			} else {
				// 更新原始商品表状态值
				updateOhFeiGoods(originalGoodsId, goodsId, head);
				log.error("该供应商的该条商品已存在");
				GoodsSupplyManageInfo goSuMaInfo = new GoodsSupplyManageInfo();
				goSuMaInfo.setResCode("0");
				goSuMaInfo.setResDescription("该供应商的该商品已存在");
				String resultBody = gson.toJson(goSuMaInfo);
				return resultBody;
			}
		}

		return null;
	}

	/**
	 * 向商品供应表，新增腾讯产品
	 * 
	 * @param info
	 * @param info1
	 * @param head
	 * @throws ServerDisponseException
	 */
	private void addTencentGoods(GoodsSupplyManageInfo info,
			GoodsSupplyManageInfo info1, Head head)
			throws ServerDisponseException {
		info1.setRechargeLimit("");
		// 商品编号
		String goodsId = info.getGoodsId();
		// 供应商编号
		String supplyId = info.getSuppliersId();
		// 原始商品编号
		String originalGoodsId = info.getOriginalGoodsId();
		info1.setSuppliersId(supplyId);
		info1.setSupplyName("腾讯");

		TencentGoodsInfo tencentGoodsInfo = null;
		// 腾讯产品，originalGoodsId属性传的值为商品名称
		tencentGoodsInfo = tencentGoodsInfoDAO
				.queryGoodsInfoGoodsId(originalGoodsId);
		if (tencentGoodsInfo != null) {
			// 进价
			String unitPriceStr = tencentGoodsInfo.getInPrice();
			int unitPrice = DataConversion.converStringToInteger(unitPriceStr);
			info1.setGoodsCostPrice(unitPrice);
			// 商品面值
			int parValue = 1000; // 腾讯产品不显示面值 ,暂时设置为1元 单位：厘
			info1.setGoodsValue(parValue);
			info1.setGoodsState("1");
			info1.setRemark("腾讯商品供应新增");

			try {
				goodsSupplyManageDAO.insert(info1);
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}

			updateTencentGoods(originalGoodsId, goodsId, head);
		} else {
			log.error("Service处理[" + head.getRequestId()
					+ "]的请求出现异常，腾讯原始商品表中该商品不存在！");
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		}

	}

	/**
	 * 向商品供应表，新增坚果网络产品
	 * 
	 * @param info
	 * @param info1
	 * @param head
	 * @throws ServerDisponseException
	 */
	private void addNutsNetGoods(GoodsSupplyManageInfo info,
			GoodsSupplyManageInfo info1, Head head)
			throws ServerDisponseException {
		info1.setRechargeLimit(""); // 坚果网络的充值数量限制在产品详细列表中，需要接口调用
		// 商品编号
		String goodsId = info.getGoodsId();
		// 供应商编号
		String supplyId = info.getSuppliersId();
		// 原始商品编号
		String originalGoodsId = info.getOriginalGoodsId();

		info1.setSuppliersId(supplyId);
		info1.setSupplyName("坚果网络");

		NutsNetGoodsInfo nutsNetGoodsInfo = null;
		nutsNetGoodsInfo = nutsNetGoodsInfoDAO.queryById(originalGoodsId);
		if (nutsNetGoodsInfo != null) {
			// 进价
			String unitPriceStr = nutsNetGoodsInfo.getUnitPrice();
			double value = Double.parseDouble(unitPriceStr);
			String priceStr = value * 1000 + "";
			String unitPrice = DataConversion.converData(priceStr);
			int goodsCostPrice = Integer.parseInt(unitPrice); // 单位：厘
			info1.setGoodsCostPrice(goodsCostPrice);
			// 商品面值
			String parValueStr = nutsNetGoodsInfo.getParvalue();
			String value1 = DataConversion.converData(parValueStr);
			int parValue = Integer.parseInt(value1) * 1000; // 单位：厘

			info1.setGoodsValue(parValue);
			info1.setGoodsState("1");
			info1.setRemark("坚果网络商品供应新增");

			try {
				goodsSupplyManageDAO.insert(info1);

			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
			// 更新原始商品表状态值
			updateNutsNetGoods(originalGoodsId, goodsId, head);

		} else {
			log.error("Service处理[" + head.getRequestId()
					+ "]的请求出现异常，坚果网络原始商品表中该商品不存在！");
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		}
	}

	/**
	 * 向商品供应表，新增殴飞产品
	 * 
	 * @param info
	 * @param info1
	 * @param head
	 * @throws ServerDisponseException
	 */
	private void addOhFeiGoods(GoodsSupplyManageInfo info,
			GoodsSupplyManageInfo info1, Head head)
			throws ServerDisponseException {
		// 商品编号
		String goodsId = info.getGoodsId();
		// 供应商编号
		String supplyId = info.getSuppliersId();
		// 原始商品编号
		String originalGoodsId = info.getOriginalGoodsId();

		info1.setSuppliersId(supplyId);
		info1.setSupplyName("殴飞");
		OhFeiGoodsInfo ohFeiGoodsInfo = null;
		ohFeiGoodsInfo = ohFeiGoodsInfoDAO.queryById(originalGoodsId);
		if (ohFeiGoodsInfo != null) {
			// 进价
			String unitPrice = ohFeiGoodsInfo.getGoodsInPrice();
			int goodsCostPrice = DataConversion
					.converStringToInteger(unitPrice);
			info1.setGoodsCostPrice(goodsCostPrice);
			// 商品面值
			String parValue = ohFeiGoodsInfo.getParValue() + "";
			int goodsValue = DataConversion.converStringToInteger(parValue);

			info1.setGoodsValue(goodsValue);
			info1.setGoodsState("1");
			info1.setRemark("殴飞商品供应新增");
			info1.setRechargeLimit(ohFeiGoodsInfo.getAmounts());
			try {
				goodsSupplyManageDAO.insert(info1);

			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
			// 更新原始商品表状态值
			updateOhFeiGoods(originalGoodsId, goodsId, head);

		} else {
			log.error("Service处理[" + head.getRequestId()
					+ "]的请求出现异常，殴飞原始商品表中该商品不存在！");
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		}

	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfInsert(GoodsSupplyManageInfo info, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		// 商品编号
		String goodsId = info.getGoodsId();
		// 供应商编号
		String supplyId = info.getSuppliersId();
		// 原始商品编号
		String originalGoodsId = info.getOriginalGoodsId();

		if ("".equals(goodsId) || goodsId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		} else if ("".equals(supplyId) || supplyId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SUPPLY_ID_NULL.getCode(),
					ExceptionCodeDes.SUPPLY_ID_NULL.getDescription());
		} else if ("".equals(originalGoodsId) || originalGoodsId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		} else {
			flag = true;
		}

		return flag;

	}

	/**
	 * 向供应商原始产品表中，加入手拉手产品编号<br>
	 * 次方法已不再使用---2012-11-13
	 * 
	 * @param info
	 */
	@SuppressWarnings("unused")
	private void insertSlsGoodsId(GoodsSupplyManageInfo info) {
		// 供应商编号
		String suppliersId = info.getSuppliersId();
		// 产品原始编号
		String originalGoodsId = info.getOriginalGoodsId();
		// 手拉手定义商品编号
		String slsGoodsId = info.getGoodsId();

		if ("20121000".equals(suppliersId)) { // 殴飞
			// updateOhFeiGoods(originalGoodsId, slsGoodsId);

		} else if ("20121001".equals(suppliersId)) { // 坚果
			// updateNutsNetGoods(originalGoodsId, slsGoodsId);

		} else if ("20121002".equals(suppliersId)) { // 腾讯()
			// ...
		}
	}

	/**
	 * 更新殴飞原始产品表
	 * 
	 * @param oriGoodsId
	 *            原始产品编号
	 * @param slsGoodsId
	 *            手拉手产品编号
	 * @return
	 * @throws ServerDisponseException
	 */
	private void updateOhFeiGoods(String oriGoodsId, String slsGoodsId,
			Head head) throws ServerDisponseException {
		OhFeiGoodsInfo ohFeiGoodsInfo = null;
		OhFeiGoodsInfo ohFeiGoodsInfo1 = new OhFeiGoodsInfo();

		ohFeiGoodsInfo = ohFeiGoodsInfoDAO.queryById(oriGoodsId);
		if (ohFeiGoodsInfo != null) {
			ohFeiGoodsInfo1.setId(ohFeiGoodsInfo.getId());
			ohFeiGoodsInfo1.setIsAudit("1"); // 0：未审核，1已审核
			ohFeiGoodsInfo1.setSlsGoodsId(slsGoodsId);
			try {
				ohFeiGoodsInfoDAO.update(ohFeiGoodsInfo1);
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		} else {
			log.error("Service处理[" + head.getRequestId()
					+ "]的请求出现异常，殴飞原始商品不存在！");
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		}
	}

	/**
	 * 更新坚果网络原始产品表
	 * 
	 * @param oriGoodsId
	 *            原始产品编号
	 * @param slsGoodsId
	 *            手拉手产品编号
	 * @throws ServerDisponseException
	 */
	private void updateNutsNetGoods(String oriGoodsId, String slsGoodsId,
			Head head) throws ServerDisponseException {
		NutsNetGoodsInfo nutsNetGoodsInfo = null;
		NutsNetGoodsInfo nutsNetGoodsInfo1 = new NutsNetGoodsInfo();

		nutsNetGoodsInfo = nutsNetGoodsInfoDAO.queryById(oriGoodsId);
		if (nutsNetGoodsInfo != null) {
			nutsNetGoodsInfo1.setId(nutsNetGoodsInfo.getId());
			nutsNetGoodsInfo1.setPartnerId(nutsNetGoodsInfo.getPartnerId());
			nutsNetGoodsInfo1.setSupProductId(nutsNetGoodsInfo
					.getSupProductId());
			nutsNetGoodsInfo1.setUnitPrice(nutsNetGoodsInfo.getUnitPrice());
			nutsNetGoodsInfo1.setName(nutsNetGoodsInfo.getName());
			nutsNetGoodsInfo1.setProductType(nutsNetGoodsInfo.getProductType());
			nutsNetGoodsInfo1.setParvalue(nutsNetGoodsInfo.getParvalue());
			nutsNetGoodsInfo1.setTs(nutsNetGoodsInfo.getTs());
			nutsNetGoodsInfo1.setSign(nutsNetGoodsInfo.getSign());
			nutsNetGoodsInfo1.setSerialNumber(nutsNetGoodsInfo
					.getSerialNumber());
			nutsNetGoodsInfo1.setOpCode(nutsNetGoodsInfo.getOpCode());
			nutsNetGoodsInfo1.setGoodsDelete(nutsNetGoodsInfo.getGoodsDelete());
			nutsNetGoodsInfo1.setIsAudit("1"); // 0：未审核，1已审核
			nutsNetGoodsInfo1.setSlsGoodsId(slsGoodsId);

			try {
				nutsNetGoodsInfoDAO.update(nutsNetGoodsInfo1);

			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		} else {
			log.error("Service处理[" + head.getRequestId()
					+ "]的请求出现异常，坚果网络原始商品不存在！");
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		}

	}

	/**
	 * 更新腾讯原始产品表
	 * 
	 * @param oriGoodsId
	 *            原始产品名称
	 * @param slsGoodsId
	 *            手拉手产品编号
	 * @throws ServerDisponseException
	 */
	private void updateTencentGoods(String oriGoodsId, String slsGoodsId,
			Head head) throws ServerDisponseException {
		TencentGoodsInfo tencentGoodsInfo = null;
		TencentGoodsInfo tencentGoodsInfo1 = new TencentGoodsInfo();

		tencentGoodsInfo = tencentGoodsInfoDAO
				.queryGoodsInfoGoodsId(oriGoodsId);
		if (tencentGoodsInfo != null) {
			tencentGoodsInfo1.setId(tencentGoodsInfo.getId());
			tencentGoodsInfo1.setProductName(tencentGoodsInfo.getProductName());
			tencentGoodsInfo1.setIn_acct_type(tencentGoodsInfo
					.getIn_acct_type());
			tencentGoodsInfo1.setSell_type(tencentGoodsInfo.getSell_type());
			tencentGoodsInfo1.setSell_sub_type(tencentGoodsInfo
					.getSell_sub_type());
			tencentGoodsInfo1.setNum(tencentGoodsInfo.getNum());
			tencentGoodsInfo1.setIsAudit("1");
			tencentGoodsInfo1.setMemo(tencentGoodsInfo.getMemo());
			tencentGoodsInfo1.setInPrice(tencentGoodsInfo.getInPrice());

			try {
				tencentGoodsInfoDAO.saveAuditGoods(tencentGoodsInfo1);

			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}

		} else {
			log.error("Service处理[" + head.getRequestId()
					+ "]的请求出现异常，腾讯原始商品不存在！");
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		}

	}

	/**
	 * 根据goodsId，查询进价最低的商品
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryMinPriceById(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		// 对消息body进行解析
		GoodsSupplyManageInfo info = gson.fromJson(body,
				GoodsSupplyManageInfo.class);
		boolean flag = checkDataOfqueryMin(info, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				String goodsId = info.getGoodsId();
				List<GoodsSupplyManageInfo> infoList = null;
				GoodsSupplyManageInfo info1 = null;
				GoodsSupplyManageInfo goodsSupplyManageInfo1 = new GoodsSupplyManageInfo();
				goodsSupplyManageInfo1.setGoodsId(goodsId);
				goodsSupplyManageInfo1.setGoodsState("1"); // 上架
				infoList = goodsSupplyManageDAO
						.queryMininunPrice(goodsSupplyManageInfo1);
				if (infoList != null && infoList.size() > 0) {
					info1 = infoList.get(0);
				}

				String resultBody = null;
				if (info1 != null) {
					resultBody = gson.toJson(info1);
				}
				return resultBody;

			} catch (Exception e) {
				log.error(e);
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 记录详细
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryByGoodsId(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		// 对消息body进行解析
		GoodsSupplyManageInfo info = gson.fromJson(body,
				GoodsSupplyManageInfo.class);
		boolean flag = checkDataOfqueryById(info, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				GoodsSupplyManageInfo info1 = null;
				info1 = goodsSupplyManageDAO.queryByGoodsIdAndSupplyId(info);

				String resultBody = null;
				if (info1 != null) {
					resultBody = gson.toJson(info1);
				}
				return resultBody;
			} catch (Exception e) {
				log.error(e);
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 记录更新(修改上下架)
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String update(String body, Head head) throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsSupplyManageInfo info = gson.fromJson(body,
				GoodsSupplyManageInfo.class);
		boolean flag = checkDataOfUpdate(info, head.getRequestId());
		if (flag) { // 数据校验通过
			// 更新的时间
			String now = ServerDateAndTime.getNowDateTimeOfFormat();
			info.setModifyTime(now);
			try {
				goodsSupplyManageDAO.updateState(info);

				// ********** 商品供应表中的某商品上架 商品信息表中对应的商品也要上架 **********
				String goodsStatus = info.getGoodsState();
				if ("1".equals(goodsStatus)) { // 上架
					pullUpShelf(info);
				}

				return null;
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	@Override
	public void updateInfo(String body, Head head)
			throws ServerDisponseException {
		// 对消息body进行解析
		GoodsSupplyManageInfo info = gson.fromJson(body,
				GoodsSupplyManageInfo.class);
		if (info == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
		if (StringUtils.isEmpty(info.getId())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ID_EMPTY.getCode(),
					ExceptionCodeDes.ID_EMPTY.getDescription());
		}
		// 更新的时间
		String now = ServerDateAndTime.getNowDateTimeOfFormat();
		info.setModifyTime(now);
		try {
			goodsSupplyManageDAO.update(info);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	/**
	 * 商品信息表，该商品上架
	 * 
	 * @param info
	 */
	private void pullUpShelf(GoodsSupplyManageInfo info) {
		GoodsManageInfo goodsManageInfo = new GoodsManageInfo();
		String goodsId = info.getGoodsId();
		goodsManageInfo.setGoodsId(goodsId);
		goodsManageInfo.setGoodsState("0"); // 下架
		GoodsManageInfo goMaInfo = null;
		goMaInfo = goodsInfoManageDAO.queryRecordByGoodsId(goodsManageInfo);
		if (goMaInfo != null) {
			try {
				goodsManageInfo.setGoodsState("1"); // 上架
				goodsManageInfo.setId(goMaInfo.getId());
				goodsInfoManageDAO.updateState(goodsManageInfo);
			} catch (Exception e) {
				log.error(e);
			}
		}
	}

	/**
	 * 根据商品编号，查询该商品最高进价
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryHighInPrice(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsSupplyManageInfo info = gson.fromJson(body,
				GoodsSupplyManageInfo.class);
		boolean flag = checkDataOfQueryHighInPrice(info, head.getRequestId());
		if (flag) { // 数据校验通过
			String highInPrice = null;
			try {
				highInPrice = goodsSupplyManageDAO.queryHighInPrice(info);
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}

			return highInPrice;
		}

		return null;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfQueryHighInPrice(GoodsSupplyManageInfo info,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		// 商品编号
		String goodsId = info.getGoodsId();
		if ("".equals(goodsId) || goodsId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfqueryMin(GoodsSupplyManageInfo info, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		String goodsId = info.getGoodsId();
		String suppliersId = info.getSuppliersId();
		if ("".equals(goodsId) || goodsId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		} else if ("".equals(suppliersId) || suppliersId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SUPPLY_ID_NULL.getCode(),
					ExceptionCodeDes.SUPPLY_ID_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfqueryById(GoodsSupplyManageInfo info,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		String id = info.getId();
		if (StringUtils.isEmpty(id)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ID_EMPTY.getCode(),
					ExceptionCodeDes.ID_EMPTY.getDescription());
		}  else {
			flag = true;
		}

		return flag;

	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfUpdate(GoodsSupplyManageInfo info, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		String goodsStatus = info.getGoodsState();
		String id = info.getId();

		if (StringUtils.isEmpty(id)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ID_EMPTY.getCode(),
					ExceptionCodeDes.ID_EMPTY.getDescription());
		} else if (StringUtils.isEmpty(goodsStatus)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;

	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfqueryByPage(GoodsSupplyManageInfo info,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		String currentPage = info.getCurrentPage() + "";
		String pageSize = info.getPageSize() + "";
		if ("".equals(currentPage) || currentPage == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.CURRENT_PAGE_NULL.getCode(),
					ExceptionCodeDes.CURRENT_PAGE_NULL.getDescription());
		} else if ("".equals(pageSize) || pageSize == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PAGE_SIZE_NULL.getCode(),
					ExceptionCodeDes.PAGE_SIZE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	@Override
	public List<String> querySupplyNameList() {
		List<String> names = null;
		try {
			names = this.goodsSupplyManageDAO.querySupplyNameList("1");
		} catch (Exception e) {
			log.error("查询供应商名称列表失败", e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return names;
	}

}
